import React from 'react';
import { Route, Redirect, RouteProps } from 'react-router-dom';
import { useAuth } from '../contexts/AuthContext';

interface PrivateRouteProps extends RouteProps {
  component: React.ComponentType<any>;
}

const PrivateRoute: React.FC<any> = ({ component: Component, ...rest }) => {
    const { user, loading } = useAuth();

    return (
        <Route
            {...rest}
            render={(props) => {
                if (loading) return <div>加载中...</div>;
                return user ? <Component {...props} /> : <Redirect to="/login" />;
            }}
        />
    );
};


export default PrivateRoute;